home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / arcer / gnutar10.lha / GnuTAR / GNUTarSource.LHA / source / port.c < prev    next >
C/C++ Source or Header  |  1991-07-04  |  27KB  |  1,289 lines

  1. /* Supporting routines which may sometimes be missing.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * @(#)port.c 1.15      87/11/05        by John Gilmore, 1986
  22.  *
  23.  * These are routines not available in all environments.
  24.  *
  25.  * I know this introduces an extra level of subroutine calls and is
  26.  * slightly slower.  Frankly, my dear, I don't give a damn.  Let the
  27.  * Missed-Em Vee losers suffer a little.  This software is proud to
  28.  * have been written on a BSD system.
  29.  */
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <signal.h>
  34. #include <errno.h>
  35.  
  36. #ifdef    MSDOS
  37. #include <fcntl.h>
  38. #else
  39. #include <sys/file.h>
  40. #endif
  41.  
  42. #include "tar.h"
  43. #include "port.h"
  44.  
  45. #ifdef USG
  46. #include <string.h>
  47. #else
  48. extern size_t strlen();
  49. #endif
  50.  
  51. extern long baserec;
  52. /*
  53.  * Some people (e.g. V7) don't have a #define for these.
  54.  */
  55. #ifndef O_BINARY
  56. #define O_BINARY    0
  57. #endif
  58. #ifndef O_RDONLY
  59. #define O_RDONLY    0
  60. #endif
  61. #ifndef NULL
  62. #define NULL 0
  63. #endif
  64.  
  65. /* JF: modified so all configuration information can appear here, instead of
  66.    being scattered through the file.  Add all the machine-dependent #ifdefs
  67.    here */
  68. #undef WANT_DUMB_GETDATE/* WANT_DUMB_GETDATE --> getdate() */
  69. #undef WANT_VALLOC    /* WANT_VALLOC --> valloc() */
  70. #undef WANT_MKDIR    /* WANT_MKDIR --> mkdir() rmdir() */
  71. #undef WANT_STRING    /* WANT_STRING --> index() bcopy() bzero() bcmp() */
  72. #undef WANT_BZERO    /* WANT_BZERO --> bzero() bcmp() execlp() */
  73.             /* EMUL_OPEN3 --> open3() */
  74. #undef WANT_MKNOD    /* WANT_MKNOD --> mknod() link() chown() geteuid() */
  75. #undef WANT_UTILS    /* WANT_UTILS --> panic() ck_*() *_buffer()
  76.                merge_sort() quote_copy_string() un_quote_string() */
  77. #undef WANT_CK_PIPE    /* WANT_CK_PIPE --> ck_pipe() */
  78. #undef WANT_GETWD    /* WANT_GETWD --> getwd() */
  79. #undef WANT_STRSTR    /* WANT_STRSTR --> strstr() */
  80. #undef WANT_FTRUNCATE    /* WANT_FTRUNCATE --> frtruncate() */
  81.  
  82. /* Define only ONE of these four . . . */
  83. /* #undef DOPRNT_MSG    /* Define this one if you have _doprnt() and
  84.                no varargs support */
  85. /* #undef VARARGS_MSG    /* Define this one if you have varargs.h and
  86.                vfprintf() */
  87. /* #undef STDC_MSG    /* Define this one if you are using ANSI C and
  88.                and have vfprintf() */
  89. /* #undef LOSING_MSG    /* Define this one if you don't have any of the
  90.                above */
  91.  
  92. #ifdef AMIGA
  93. #define WANT_DUMB_GETDATE/* WANT_DUMB_GETDATE --> getdate() */
  94. #define WANT_VALLOC     /* WANT_VALLOC --> valloc() */
  95. #define WANT_MKNOD     /* WANT_MKNOD --> mknod() link() chown() geteuid() */
  96. #endif
  97.  
  98.  
  99. #ifdef USG
  100. #define WANT_STRING
  101. #define WANT_VALLOC
  102.  
  103. #if defined(sgi) && defined(mips)
  104. #define WANT_GETWD
  105. #endif
  106.  
  107. #if defined(i386)
  108. #define WANT_FTRUNCATE
  109. #endif
  110.  
  111. #endif
  112.  
  113. #ifdef MINIX
  114. #define WANT_BZERO
  115. #endif
  116.  
  117. #ifdef MSDOS
  118. char TTY_NAME[] = "con";
  119.  
  120. #define WANT_STRING
  121. #define WANT_MKNOD
  122. #define WANT_UTILS
  123. #define WANT_VALLOC
  124.  
  125. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  126. #ifdef __STDC__
  127. #define STDC_MSG
  128. #else
  129. #define LOSING_MSG
  130. #endif
  131. #endif
  132.  
  133. #else /* not MSDOS */
  134. char TTY_NAME[] ="/dev/tty";
  135.  
  136. #define WANT_UTILS
  137. #define WANT_CK_PIPE
  138. #define WANT_STRSTR
  139.  
  140. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  141. #ifdef BSD42
  142. /* BSD systems should do this even if __STDC__, because
  143.    we might be using an ANSI compiler without an ANSI library. (sigh) */
  144. #ifdef sparc
  145. #define LOSING_MSG
  146. #else
  147. #define DOPRNT_MSG
  148. #endif
  149. #else /* not BSD */
  150. #ifdef __STDC__
  151. #define STDC_MSG
  152. #else /* not ANSI C */
  153. #define LOSING_MSG
  154. #endif /* not ANSI C */
  155. #endif /* not BSD */
  156. #endif /* Need to define some form of _MSG */
  157. #endif /* not MSDOS */
  158.  
  159. /* End of system-dependent #ifdefs */
  160.  
  161. #ifdef WANT_DUMB_GETDATE
  162. /* JF a getdate() routine takes a date/time/etc and turns it into a time_t */
  163. /* This one is a quick hack I wrote in about five minutes to see if the N
  164.    option works.  Someone should replace it with one that works */
  165.  
  166. /* This getdate takes an arg of the form mm/dd/yyyy hh:mm:ss and turns it
  167.    into a time_t .  Its not well tested or anything. . .  */
  168. /* In general, you should use the getdate() supplied in getdate.y */
  169.  
  170. #define OFF_FROM GMT 18000        /* Change for your time zone! */
  171.  
  172. time_t
  173. getdate(str)
  174. char *str;
  175. {
  176.     int month,day,year,hour,minute,second;
  177.     time_t ret;
  178.     int    n;
  179.  
  180. #define SECS_PER_YEAR (365L*SECS_PER_DAY)
  181. #define SECS_PER_LEAP_YEAR (366L*SECS_PER_DAY)
  182.  
  183. #define SECS_PER_DAY (24L*60*60)
  184.     static int days_per_month[2][12] = {
  185.         31,28,31,30,31,30,31,31,30,31,30,31,
  186.         31,29,31,30,31,30,31,31,30,31,30,31
  187.     };
  188.  
  189.     static int days_per_year[2]={365,366};
  190.  
  191.     month=day=year=hour=minute=second=0;
  192.     n=sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second);
  193.     if(n<3)
  194.         return 0;
  195.     if(year<100)
  196.         year+=1900;
  197.     if(year<1970)
  198.         return 0;
  199.     ret=0;
  200.  
  201. #ifndef AMIGA
  202.     ret+=OFF_FROM_GMT;
  203. #endif
  204.  
  205.     for(n=1970;n<year;n++)
  206.         if(n%4==0 && n%400!=0)
  207.             ret+=SECS_PER_LEAP_YEAR;
  208.         else
  209.             ret+=SECS_PER_YEAR;
  210.  
  211.     month--;
  212.     for(n=0;n<month;n++) {
  213.         if(year%4==0 && year%400!=0)
  214.             ret+=SECS_PER_DAY*days_per_month[1][n];
  215.         else
  216.             ret+=SECS_PER_DAY*days_per_month[0][n];
  217.     }
  218.     ret+=SECS_PER_DAY*(day-1);
  219.     ret+=second+minute*60+hour*60*60;
  220.     return ret;
  221. }
  222. #endif
  223.  
  224. #ifdef WANT_VALLOC
  225. /*
  226.  * valloc() does a malloc() on a page boundary.  On some systems,
  227.  * this can make large block I/O more efficient.
  228.  */
  229. char *
  230. valloc (size)
  231.     unsigned size;
  232. {
  233.     extern char *malloc ();
  234.     return (malloc (size));
  235. }
  236. #endif
  237. /*
  238.  *                NMKDIR.C
  239.  *
  240.  * Written by Robert Rother, Mariah Corporation, August 1985.
  241.  *
  242.  * I wrote this out of shear disgust with myself because I couldn't
  243.  * figure out how to do this in /bin/sh.
  244.  *
  245.  * If you want it, it's yours.  All I ask in return is that if you
  246.  * figure out how to do this in a Bourne Shell script you send me
  247.  * a copy.
  248.  *                    sdcsvax!rmr or rmr@uscd
  249. *
  250. * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  251. * subroutine.    11Mar86; hoptoad!gnu
  252. *
  253. * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  254. * subroutine didn't return EEXIST.  It does now.
  255. */
  256.  
  257. /*
  258.  * Make a directory.  Compatible with the mkdir() system call on 4.2BSD.
  259.  */
  260. #ifdef WANT_MKDIR
  261. int
  262. mkdir(dpath, dmode)
  263.     char *dpath;
  264.     int dmode;
  265. {
  266.     int cpid, status;
  267.     struct stat statbuf;
  268.     extern int errno;
  269.  
  270.     if (stat(dpath,&statbuf) == 0) {
  271.         errno = EEXIST;     /* Stat worked, so it already exists */
  272.         return -1;
  273.     }
  274.  
  275.     /* If stat fails for a reason other than non-existence, return error */
  276.     if (errno != ENOENT) return -1;
  277.  
  278.     switch (cpid = fork()) {
  279.  
  280.     case -1:            /* Error in fork() */
  281.         return(-1);             /* Errno is set already */
  282.  
  283.     case 0:             /* Child process */
  284.         /*
  285.          * Cheap hack to set mode of new directory.  Since this
  286.          * child process is going away anyway, we zap its umask.
  287.          * FIXME, this won't suffice to set SUID, SGID, etc. on this
  288.          * directory.  Does anybody care?
  289.          */
  290.         status = umask(0);      /* Get current umask */
  291.         status = umask(status | (0777 & ~dmode)); /* Set for mkdir */
  292.         execl("/bin/mkdir", "mkdir", dpath, (char *)0);
  293.         _exit(-1);              /* Can't exec /bin/mkdir */
  294.  
  295.     default:            /* Parent process */
  296.         while (cpid != wait(&status)) ; /* Wait for kid to finish */
  297.     }
  298.  
  299.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  300.         errno = EIO;        /* We don't know why, but */
  301.         return -1;        /* /bin/mkdir failed */
  302.     }
  303.  
  304.     return 0;
  305. }
  306. int
  307. rmdir(dpath)
  308.     char *dpath;
  309. {
  310.     int cpid, status;
  311.     struct stat statbuf;
  312.     extern int errno;
  313.  
  314.     if (stat(dpath,&statbuf) != 0) {
  315.         /* Stat just set errno.  We don't have to */
  316.         return -1;
  317.     }
  318.  
  319.     switch (cpid = fork()) {
  320.  
  321.     case -1:            /* Error in fork() */
  322.         return(-1);             /* Errno is set already */
  323.  
  324.     case 0:             /* Child process */
  325.         execl("/bin/rmdir", "rmdir", dpath, (char *)0);
  326.         _exit(-1);              /* Can't exec /bin/mkdir */
  327.  
  328.     default:            /* Parent process */
  329.         while (cpid != wait(&status)) ; /* Wait for kid to finish */
  330.     }
  331.  
  332.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  333.         errno = EIO;        /* We don't know why, but */
  334.         return -1;        /* /bin/mkdir failed */
  335.     }
  336.  
  337.     return 0;
  338. }
  339. #endif
  340.  
  341. #ifdef WANT_STRING
  342. /*
  343.  * Translate V7 style into Sys V style.
  344.  */
  345. #include <string.h>
  346. #include <memory.h>
  347.  
  348. char *
  349. index (s, c)
  350.     char *s;
  351.     int c;
  352. {
  353.     return (strchr (s, c));
  354. }
  355.  
  356. char *
  357. rindex(s,c)
  358. char *s;
  359. int c;
  360. {
  361.     return strrchr(s,c);
  362. }
  363.  
  364. void
  365. bcopy (s1, s2, n)
  366.     char *s1, *s2;
  367.     int n;
  368. {
  369.     (void) memcpy (s2, s1, n);
  370. }
  371.  
  372. void
  373. bzero (s1, n)
  374.     char *s1;
  375.     int n;
  376. {
  377.     (void) memset(s1, 0, n);
  378. }
  379.  
  380. int
  381. bcmp(s1, s2, n)
  382.     char    *s1, *s2;
  383.     int    n;
  384. {
  385.     return memcmp(s1, s2, n);
  386. }
  387. #endif
  388.  
  389. #ifdef WANT_BZERO
  390. /* Minix has bcopy but not bzero, and no memset.  Thanks, Andy. */
  391. void
  392. bzero (s1, n)
  393.     register char *s1;
  394.     register int n;
  395. {
  396.     while (n--) *s1++ = '\0';
  397. }
  398.  
  399. /* It also has no bcmp() */
  400. int
  401. bcmp (s1, s2, n)
  402.     register char *s1,*s2;
  403.     register int n;
  404. {
  405.     for ( ; n-- ; ++s1, ++s2) {
  406.         if (*s1 != *s2) return *s1 - *s2;
  407.     }
  408.     return 0;
  409. }
  410.  
  411. /*
  412.  * Groan, Minix doesn't have execlp either!
  413.  *
  414.  * execlp(file,arg0,arg1...argn,(char *)NULL)
  415.  * exec a program, automatically searching for the program through
  416.  * all the directories on the PATH.
  417.  *
  418.  * This version is naive about variable argument lists, it assumes
  419.  * a straightforward C calling sequence.  If your system has odd stacks
  420.  * *and* doesn't have execlp, YOU get to fix it.
  421.  */
  422. int
  423. execlp(filename, arg0)
  424.     char *filename, *arg0;
  425. {
  426.     register char *p, *path;
  427.     register char *fnbuffer;
  428.     char **argstart = &arg0;
  429.     struct stat statbuf;
  430.     extern char **environ;
  431.     extern int errno;
  432.     extern char *malloc(), *getenv(), *index();
  433.  
  434.     if ((p = getenv("PATH")) == NULL) {
  435.         /* couldn't find path variable -- try to exec given filename */
  436.         return execve(filename, argstart, environ);
  437.     }
  438.  
  439.     /*
  440.      * make a place to build the filename.    We malloc larger than we
  441.      * need, but we know it will fit in this.
  442.      */
  443.     fnbuffer = malloc( strlen(p) + 1 + strlen(filename) );
  444.     if (fnbuffer == NULL) {
  445.         errno = ENOMEM;
  446.         return -1;
  447.     }
  448.  
  449.     /*
  450.      * try each component of the path to see if the file's there
  451.      * and executable.
  452.      */
  453.     for (path = p ; path ; path = p) {
  454.         /* construct full path name to try */
  455.         if ((p = index(path,':')) == NULL) {
  456.             strcpy(fnbuffer, path);
  457.         } else {
  458.             strncpy(fnbuffer, path, p-path);
  459.             fnbuffer[p-path] = '\0';
  460.             p++;        /* Skip : for next time */
  461.         }
  462.         if (strlen(fnbuffer) != 0)
  463.             strcat(fnbuffer,"/");
  464.         strcat(fnbuffer,filename);
  465.  
  466.         /* check to see if file is there and is a normal file */
  467.         if (stat(fnbuffer, &statbuf) < 0) {
  468.             if (errno == ENOENT)
  469.                 continue; /* file not there,keep on looking */
  470.             else
  471.                 goto fail; /* failed for some reason, return */
  472.         }
  473.         if ( (statbuf.st_mode & S_IFMT) != S_IFREG) continue;
  474.  
  475.         if (execve(fnbuffer, argstart, environ) < 0
  476.             && errno != ENOENT
  477.             && errno != ENOEXEC) {
  478.             /* failed, for some other reason besides "file
  479.              * not found" or "not a.out format"
  480.              */
  481.             goto fail;
  482.         }
  483.  
  484.         /*
  485.          * If we got error ENOEXEC, the file is executable but is
  486.          * not an object file.    Try to execute it as a shell script,
  487.          * returning error if we can't execute /bin/sh.
  488.          *
  489.          * FIXME, this code is broken in several ways.    Shell
  490.          * scripts should not in general be executed by the user's
  491.          * SHELL variable program.  On more mature systems, the
  492.          * script can specify with #!/bin/whatever.  Also, this
  493.          * code clobbers argstart[-1] if the exec of the shell
  494.          * fails.
  495.          */
  496.         if (errno == ENOEXEC) {
  497.             char *shell;
  498.  
  499.             /* Try to execute command "sh arg0 arg1 ..." */
  500.             if ((shell = getenv("SHELL")) == NULL)
  501.                 shell = "/bin/sh";
  502.             argstart[-1] = shell;
  503.             argstart[0] = fnbuffer;
  504.             execve(shell, &argstart[-1], environ);
  505.             goto fail;    /* Exec didn't work */
  506.         }
  507.  
  508.         /*
  509.          * If we succeeded, the execve() doesn't return, so we
  510.          * can only be here is if the file hasn't been found yet.
  511.          * Try the next place on the path.
  512.          */
  513.     }
  514.  
  515.     /* all attempts failed to locate the file.  Give up. */
  516.     errno = ENOENT;
  517.  
  518. fail:
  519.     free(fnbuffer);
  520.     return -1;
  521. }
  522. #endif
  523.  
  524.  
  525. #ifdef EMUL_OPEN3
  526. #include "open3.h"
  527. /*
  528.  * open3 -- routine to emulate the 3-argument open system
  529.  * call that is present in most modern Unix systems.
  530.  * This version attempts to support all the flag bits except for O_NDELAY
  531.  * and O_APPEND, which are silently ignored.  The emulation is not as efficient
  532.  * as the real thing (at worst, 4 system calls instead of one), but there's
  533.  * not much I can do about that.
  534.  *
  535.  * Written 6/10/87 by rmtodd@uokmax
  536.  *
  537.  * open3(path, flag, mode)
  538.  * Attempts to open the file specified by
  539.  * the given pathname.    The following flag bits (#defined in tar.h)
  540.  * specify options to the routine:
  541.  *    O_RDONLY    file open for read only
  542.  *    O_WRONLY    file open for write only
  543.  *    O_RDWR        file open for both read & write
  544.  * (Needless to say, you should only specify one of the above).
  545.  *    O_CREAT     file is created with specified mode if it needs to be.
  546.  *    O_TRUNC     if file exists, it is truncated to 0 bytes
  547.  *    O_EXCL        used with O_CREAT--routine returns error if file exists
  548.  * Function returns file descriptor if successful, -1 and errno if not.
  549.  */
  550.  
  551. /*
  552.  * array to give arguments to access for various modes
  553.  * FIXME, this table depends on the specific integer values of O_XXX,
  554.  * and also contains integers (args to 'access') that should be #define's.
  555.  */
  556. static int modes[] =
  557.     {
  558.         04, /* O_RDONLY */
  559.         02, /* O_WRONLY */
  560.         06, /* O_RDWR */
  561.         06, /* invalid but we'd better cope -- O_WRONLY+O_RDWR */
  562.     };
  563.  
  564. /* Shut off the automatic emulation of open(), we'll need it. */
  565. #undef open
  566.  
  567. int
  568. open3(path, flags, mode)
  569. char *path;
  570. int flags, mode;
  571. {
  572.     extern int errno;
  573.     int exists = 1;
  574.     int call_creat = 0;
  575.     int fd;
  576.     /*
  577.      * We actually do the work by calling the open() or creat() system
  578.      * call, depending on the flags.  Call_creat is true if we will use
  579.      * creat(), false if we will use open().
  580.      */
  581.  
  582.     /*
  583.      * See if the file exists and is accessible in the requested mode.
  584.      *
  585.      * Strictly speaking we shouldn't be using access, since access checks
  586.      * against real uid, and the open call should check against euid.
  587.      * Most cases real uid == euid, so it won't matter.   FIXME.
  588.      * FIXME, the construction "flags & 3" and the modes table depends
  589.      * on the specific integer values of the O_XXX #define's.  Foo!
  590.      */
  591.     if (access(path,modes[flags & 3]) < 0) {
  592.         if (errno == ENOENT) {
  593.             /* the file does not exist */
  594.             exists = 0;
  595.         } else {
  596.             /* probably permission violation */
  597.             if (flags & O_EXCL) {
  598.                 /* Oops, the file exists, we didn't want it. */
  599.                 /* No matter what the error, claim EEXIST. */
  600.                 errno = EEXIST;
  601.             }
  602.             return -1;
  603.         }
  604.     }
  605.  
  606.     /* if we have the O_CREAT bit set, check for O_EXCL */
  607.     if (flags & O_CREAT) {
  608.         if ((flags & O_EXCL) && exists) {
  609.             /* Oops, the file exists and we didn't want it to. */
  610.             errno = EEXIST;
  611.             return -1;
  612.         }
  613.         /*
  614.          * If the file doesn't exist, be sure to call creat() so that
  615.          * it will be created with the proper mode.
  616.          */
  617.         if (!exists) call_creat = 1;
  618.     } else {
  619.         /* If O_CREAT isn't set and the file doesn't exist, error. */
  620.         if (!exists) {
  621.             errno = ENOENT;
  622.             return -1;
  623.         }
  624.     }
  625.  
  626.     /*
  627.      * If the O_TRUNC flag is set and the file exists, we want to call
  628.      * creat() anyway, since creat() guarantees that the file will be
  629.      * truncated and open()-for-writing doesn't.
  630.      * (If the file doesn't exist, we're calling creat() anyway and the
  631.      * file will be created with zero length.)
  632.      */
  633.     if ((flags & O_TRUNC) && exists) call_creat = 1;
  634.     /* actually do the call */
  635.     if (call_creat) {
  636.         /*
  637.          * call creat.    May have to close and reopen the file if we
  638.          * want O_RDONLY or O_RDWR access -- creat() only gives
  639.          * O_WRONLY.
  640.          */
  641.         fd = creat(path,mode);
  642.         if (fd < 0 || (flags & O_WRONLY)) return fd;
  643.         if (close(fd) < 0) return -1;
  644.         /* Fall out to reopen the file we've created */
  645.     }
  646.  
  647.     /*
  648.      * calling old open, we strip most of the new flags just in case.
  649.      */
  650.     return open(path, flags & (O_RDONLY|O_WRONLY|O_RDWR|O_BINARY));
  651. }
  652. #endif
  653.  
  654. #ifdef    WANT_MKNOD
  655. /* Fake mknod by complaining */
  656. int
  657. mknod(path, mode, dev)
  658.     char        *path;
  659.     unsigned short    mode;
  660.     dev_t        dev;
  661. {
  662.     extern int    errno;
  663.     int        fd;
  664.  
  665.     errno = ENXIO;        /* No such device or address */
  666.     return -1;        /* Just give an error */
  667. }
  668.  
  669. /* Fake links by copying */
  670. int
  671. link(path1, path2)
  672.     char        *path1;
  673.     char        *path2;
  674. {
  675.     char    buf[256];
  676.     int    ifd, ofd;
  677.     int    nrbytes;
  678.     int    nwbytes;
  679.  
  680.     fprintf(stderr, "%s: %s: cannot link to %s, copying instead\n",
  681.         tar, path1, path2);
  682.     if ((ifd = open(path1, O_RDONLY|O_BINARY)) < 0)
  683.         return -1;
  684.     if ((ofd = creat(path2, 0666)) < 0)
  685.         return -1;
  686.     setmode(ofd, O_BINARY);
  687.     while ((nrbytes = read(ifd, buf, sizeof(buf))) > 0) {
  688.         if ((nwbytes = write(ofd, buf, nrbytes)) != nrbytes) {
  689.             nrbytes = -1;
  690.             break;
  691.         }
  692.     }
  693.     /* Note use of "|" rather than "||" below: we want to close
  694.      * the files even if an error occurs.
  695.      */
  696.     if ((nrbytes < 0) | (0 != close(ifd)) | (0 != close(ofd))) {
  697.         unlink(path2);
  698.         return -1;
  699.     }
  700.     return 0;
  701. }
  702.  
  703. /* everyone owns everything on MS-DOS (or is it no one owns anything?) */
  704. int
  705. chown(path, uid, gid)
  706.     char    *path;
  707.     int    uid;
  708.     int    gid;
  709. {
  710.     return 0;
  711. }
  712.  
  713. int
  714. geteuid()
  715. {
  716.     return 0;
  717. }
  718. #endif    /* WANT_MKNOD */
  719.  
  720. #ifdef WANT_UTILS
  721. /* Stash argv[0] here so panic will know what the program is called */
  722. char *myname = 0;
  723.  
  724. void
  725. panic(s)
  726. char *s;
  727. {
  728.     if(myname)
  729.         fprintf(stderr,"%s:",myname);
  730.     fprintf(stderr,s);
  731.     putc('\n',stderr);
  732.     exit(12);
  733. }
  734.  
  735.  
  736. char *
  737. ck_malloc(size)
  738. size_t size;
  739. {
  740.     char *ret;
  741.     char *malloc();
  742.  
  743.     if(!size)
  744.         size++;
  745.     ret=malloc(size);
  746.     if(ret==0)
  747.         panic("Couldn't allocate memory");
  748.     return ret;
  749. }
  750.  
  751. char *
  752. ck_realloc(ptr,size)
  753. char *ptr;
  754. size_t size;
  755. {
  756.     char *ret;
  757.     char *realloc();
  758.  
  759.     if(!ptr)
  760.         ret=ck_malloc(size);
  761.     else
  762.         ret=realloc(ptr,size);
  763.     if(ret==0)
  764.         panic("Couldn't re-allocate memory");
  765.     return ret;
  766. }
  767.  
  768. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  769.    nor do we care, as long as it doesn't mind being aligned on a char boundry.
  770.  */
  771.  
  772. struct buffer {
  773.     int    allocated;
  774.     int    length;
  775.     char    *b;
  776. };
  777.  
  778. #define MIN_ALLOCATE 50
  779.  
  780. char *
  781. init_buffer()
  782. {
  783.     struct buffer *b;
  784.  
  785.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  786.     b->allocated=MIN_ALLOCATE;
  787.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  788.     b->length=0;
  789.     return (char *)b;
  790. }
  791.  
  792. void
  793. flush_buffer(bb)
  794. char *bb;
  795. {
  796.     struct buffer *b;
  797.  
  798.     b=(struct buffer *)bb;
  799.     free(b->b);
  800.     b->b=0;
  801.     b->allocated=0;
  802.     b->length=0;
  803.     free((void *)b);
  804. }
  805.  
  806. void
  807. add_buffer(bb,p,n)
  808. char *bb;
  809. char *p;
  810. int n;
  811. {
  812.     struct buffer *b;
  813.  
  814.     b=(struct buffer *)bb;
  815.     if(b->length+n>b->allocated) {
  816.         b->allocated*=2;
  817.         b->b=(char *)ck_realloc(b->b,b->allocated);
  818.     }
  819.     bcopy(p,b->b+b->length,n);
  820.     b->length+=n;
  821. }
  822.  
  823. char *
  824. get_buffer(bb)
  825. char *bb;
  826. {
  827.     struct buffer *b;
  828.  
  829.     b=(struct buffer *)bb;
  830.     return b->b;
  831. }
  832.  
  833. char *
  834. merge_sort(list,n,off,cmp)
  835. char *list;
  836. int (*cmp)();
  837. unsigned n;
  838. int off;
  839. {
  840.     char *ret;
  841.  
  842.     char *alist,*blist;
  843.     unsigned alength,blength;
  844.  
  845.     char *tptr;
  846.     int tmp;
  847.     char **prev;
  848. #define NEXTOF(ptr)     (* ((char **)(((char *)(ptr))+off) ) )
  849.     if(n==1)
  850.         return list;
  851.     if(n==2) {
  852.         if((*cmp)(list,NEXTOF(list))>0) {
  853.             ret=NEXTOF(list);
  854.             NEXTOF(ret)=list;
  855.             NEXTOF(list)=0;
  856.             return ret;
  857.         }
  858.         return list;
  859.     }
  860.     alist=list;
  861.     alength=(n+1)/2;
  862.     blength=n/2;
  863.     for(tptr=list,tmp=(n-1)/2;tmp;tptr=NEXTOF(tptr),tmp--)
  864.         ;
  865.     blist=NEXTOF(tptr);
  866.     NEXTOF(tptr)=0;
  867.  
  868.     alist=merge_sort(alist,alength,off,cmp);
  869.     blist=merge_sort(blist,blength,off,cmp);
  870.     prev = &ret;
  871.     for(;alist && blist;) {
  872.         if((*cmp)(alist,blist)<0) {
  873.             tptr=NEXTOF(alist);
  874.             *prev = alist;
  875.             prev = &(NEXTOF(alist));
  876.             alist=tptr;
  877.         } else {
  878.             tptr=NEXTOF(blist);
  879.             *prev = blist;
  880.             prev = &(NEXTOF(blist));
  881.             blist=tptr;
  882.         }
  883.     }
  884.     if(alist)
  885.         *prev = alist;
  886.     else
  887.         *prev = blist;
  888.  
  889.     return ret;
  890. }
  891.  
  892. void
  893. ck_close(fd)
  894. int fd;
  895. {
  896.     if(close(fd)<0) {
  897.         msg_perror("can't close a file #%d",fd);
  898.         exit(EX_SYSTEM);
  899.     }
  900. }
  901.  
  902. #include <ctype.h>
  903.  
  904. /* Quote_copy_string is like quote_string, but instead of modifying the
  905.    string in place, it malloc-s a copy    of the string, and returns that.
  906.    If the string does not have to be quoted, it returns the NULL string.
  907.    The allocated copy can, of course, be freed with free() after the
  908.    caller is done with it.
  909.  */
  910. char *
  911. quote_copy_string(string)
  912. char *string;
  913. {
  914.     char    *from_here;
  915.     char    *to_there = 0;
  916.     char    *copy_buf = 0;
  917.     int    c;
  918.     int    copying = 0;
  919.  
  920.     from_here=string;
  921.     while(*from_here) {
  922.         c= *from_here++;
  923.         if(c=='\\') {
  924.             if(!copying) {
  925.                 int n;
  926.  
  927.                 n=(from_here-string)-1;
  928.                 copying++;
  929.                 copy_buf=(char *)malloc(n+5+strlen(from_here)*4);
  930.                 if(!copy_buf)
  931.                     return 0;
  932.                 bcopy(string,copy_buf,n);
  933.                 to_there=copy_buf+n;
  934.             }
  935.             *to_there++='\\';
  936.             *to_there++='\\';
  937.         } else if(isprint(c)) {
  938.             if(copying)
  939.                 *to_there++= c;
  940.         } else {
  941.             if(!copying) {
  942.                 int    n;
  943.  
  944.                 n=(from_here-string)-1;
  945.                 copying++;
  946.                 copy_buf=(char *)malloc(n+5+strlen(from_here)*4);
  947.                 if(!copy_buf)
  948.                     return 0;
  949.                 bcopy(string,copy_buf,n);
  950.                 to_there=copy_buf+n;
  951.             }
  952.             *to_there++='\\';
  953.             if(c=='\n') *to_there++='n';
  954.             else if(c=='\t') *to_there++='t';
  955.             else if(c=='\f') *to_there++='f';
  956.             else if(c=='\b') *to_there++='b';
  957.             else if(c=='\r') *to_there++='r';
  958.             else if(c=='\177') *to_there++='?';
  959.             else {
  960.                 to_there[0]=(c>>6)+'0';
  961.                 to_there[1]=((c>>3)&07)+'0';
  962.                 to_there[2]=(c&07)+'0';
  963.                 to_there+=3;
  964.             }
  965.         }
  966.     }
  967.     if(copying) {
  968.         *to_there='\0';
  969.         return copy_buf;
  970.     }
  971.     return (char *)0;
  972. }
  973.  
  974.  
  975. /* Un_quote_string takes a quoted c-string (like those produced by
  976.    quote_string or quote_copy_string and turns it back into the
  977.    un-quoted original.    This is done in place.
  978.  */
  979.  
  980. /* There is no un-quote-copy-string.  Write it yourself */
  981.  
  982. char *un_quote_string(string)
  983. char *string;
  984. {
  985.     char *ret;
  986.     char *from_here;
  987.     char *to_there;
  988.     int    tmp;
  989.  
  990.     ret=string;
  991.     to_there=string;
  992.     from_here=string;
  993.     while(*from_here) {
  994.         if(*from_here!='\\') {
  995.             if(from_here!=to_there)
  996.                 *to_there++= *from_here++;
  997.             else
  998.                 from_here++,to_there++;
  999.             continue;
  1000.         }
  1001.         switch(*++from_here) {
  1002.         case '\\':
  1003.             *to_there++= *from_here++;
  1004.             break;
  1005.         case 'n':
  1006.             *to_there++= '\n';
  1007.             from_here++;
  1008.             break;
  1009.         case 't':
  1010.             *to_there++= '\t';
  1011.             from_here++;
  1012.             break;
  1013.         case 'f':
  1014.             *to_there++= '\f';
  1015.             from_here++;
  1016.             break;
  1017.         case 'b':
  1018.             *to_there++= '\b';
  1019.             from_here++;
  1020.             break;
  1021.         case 'r':
  1022.             *to_there++= '\r';
  1023.             from_here++;
  1024.             break;
  1025.         case '?':
  1026.             *to_there++= 0177;
  1027.             from_here++;
  1028.             break;
  1029.         case '0':
  1030.         case '1':
  1031.         case '2':
  1032.         case '3':
  1033.         case '4':
  1034.         case '5':
  1035.         case '6':
  1036.         case '7':
  1037.             tmp= *from_here - '0';
  1038.             from_here++;
  1039.             if(*from_here<'0' || *from_here>'7') {
  1040.                 *to_there++= tmp;
  1041.                 break;
  1042.             }
  1043.             tmp= tmp*8 + *from_here-'0';
  1044.             from_here++;
  1045.             if(*from_here<'0' || *from_here>'7') {
  1046.                 *to_there++= tmp;
  1047.                 break;
  1048.             }
  1049.             tmp=tmp*8 + *from_here-'0';
  1050.             from_here++;
  1051.             *to_there=tmp;
  1052.             break;
  1053.         default:
  1054.             ret=0;
  1055.             *to_there++='\\';
  1056.             *to_there++= *from_here++;
  1057.             break;
  1058.         }
  1059.     }
  1060.     *to_there++='\0';
  1061.     return ret;
  1062. }
  1063. #endif
  1064.  
  1065. #ifdef WANT_CK_PIPE
  1066. void ck_pipe(pipes)
  1067. int *pipes;
  1068. {
  1069.     if(pipe(pipes)<0) {
  1070.         msg_perror("can't open a pipe");
  1071.         exit(EX_SYSTEM);
  1072.     }
  1073. }
  1074.  
  1075. #endif
  1076.  
  1077. #ifdef WANT_GETWD
  1078. char *
  1079. getwd(path)
  1080. char *path;
  1081. {
  1082.     FILE *fp;
  1083.     FILE *popen();
  1084.  
  1085.     fp=popen("pwd","r");
  1086.     if(!fp)
  1087.         return 0;
  1088.     if(!fgets(path,100,fp))
  1089.         return 0;
  1090.     if(!pclose(fp))
  1091.         return 0;
  1092.     return path;
  1093. }
  1094. #endif /* WANT_CK_PIPE */
  1095.  
  1096.  
  1097. #ifdef WANT_STRSTR
  1098.  
  1099. /*
  1100.  * strstr - find first occurrence of wanted in s
  1101.  */
  1102.  
  1103. char *                /* found string, or NULL if none */
  1104. strstr(s, wanted)
  1105. char *s;
  1106. char *wanted;
  1107. {
  1108.     register char *scan;
  1109.     register size_t len;
  1110.     register char firstc;
  1111.     extern int strcmp();
  1112.  
  1113.     /*
  1114.      * The odd placement of the two tests is so "" is findable.
  1115.      * Also, we inline the first char for speed.
  1116.      * The ++ on scan has been moved down for optimization.
  1117.      */
  1118.     firstc = *wanted;
  1119.     len = strlen(wanted);
  1120.     for (scan = s; *scan != firstc || strncmp(scan, wanted, len) != 0; )
  1121.         if (*scan++ == '\0')
  1122.             return (char *)0;
  1123.     return scan;
  1124. }
  1125. #endif
  1126.  
  1127. #ifdef WANT_FTRUNCATE
  1128.  
  1129. int
  1130. ftruncate(fd, length)
  1131. int fd;
  1132. off_t length;
  1133. {
  1134.     errno = EIO;
  1135.     return -1;
  1136. }
  1137.  
  1138. #endif
  1139.  
  1140.  
  1141.  
  1142.  
  1143. extern FILE *msg_file;
  1144.  
  1145. #ifdef STDC_MSG
  1146. #include <stdarg.h>
  1147.  
  1148. void
  1149. msg(char *str,...)
  1150. {
  1151.     va_list args;
  1152.  
  1153.     va_start(args,str);
  1154.     fflush(msg_file);
  1155.     fprintf(stderr,"%s: ",tar);
  1156.     if(f_sayblock)
  1157.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1158.     vfprintf(stderr,str,args);
  1159.     va_end(args);
  1160.     putc('\n',stderr);
  1161.     fflush(stderr);
  1162. }
  1163.  
  1164. void
  1165. msg_perror(char *str,...)
  1166. {
  1167.     va_list args;
  1168.     int save_e;
  1169.     extern int errno;
  1170.  
  1171.     save_e=errno;
  1172.     fflush(msg_file);
  1173.     fprintf(stderr,"%s: ",tar);
  1174.     if(f_sayblock)
  1175.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1176.     va_start(args,str);
  1177.     vfprintf(stderr,str,args);
  1178.     va_end(args);
  1179.     errno=save_e;
  1180.     perror(" ");
  1181.     fflush(stderr);
  1182. }
  1183. #endif
  1184.  
  1185. #ifdef VARARGS_MSG
  1186. #include <varargs.h>
  1187. void msg(str,va_alist)
  1188. char *str;
  1189. va_dcl
  1190. {
  1191.     va_list args;
  1192.  
  1193.     fflush(msg_file);
  1194.     fprintf(stderr,"%s: ",tar);
  1195.     if(f_sayblock)
  1196.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1197.     va_start(args);
  1198.     vfprintf(stderr,str,args);
  1199.     va_end(args);
  1200.     putc('\n',stderr);
  1201.     fflush(stderr);
  1202. }
  1203.  
  1204. void msg_perror(str,va_alist)
  1205. char *str;
  1206. va_dcl
  1207. {
  1208.     va_list args;
  1209.     int save_e;
  1210.     extern int errno;
  1211.  
  1212.     save_e=errno;
  1213.     fflush(msg_file);
  1214.     fprintf(stderr,"%s: ",tar);
  1215.     if(f_sayblock)
  1216.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1217.     va_start(args);
  1218.     vfprintf(stderr,str,args);
  1219.     va_end(args);
  1220.     errno=save_e;
  1221.     perror(" ");
  1222.     fflush(stderr);
  1223. }
  1224. #endif
  1225.  
  1226. #ifdef DOPRNT_MSG
  1227. void msg(str,args)
  1228. char *str;
  1229. int args;
  1230. {
  1231.     fflush(msg_file);
  1232.     fprintf(stderr,"%s: ",tar);
  1233.     if(f_sayblock)
  1234.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1235.     _doprnt(str, &args, stderr);
  1236.     putc('\n',stderr);
  1237.     fflush(stderr);
  1238. }
  1239.  
  1240. void msg_perror(str,args)
  1241. char *str;
  1242. {
  1243.     int save_e;
  1244.     extern int errno;
  1245.  
  1246.     save_e=errno;
  1247.     fflush(msg_file);
  1248.     fprintf(stderr,"%s: ",tar);
  1249.     if(f_sayblock)
  1250.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1251.     _doprnt(str, &args, stderr);
  1252.     errno=save_e;
  1253.     perror(" ");
  1254.     fflush(stderr);
  1255. }
  1256.  
  1257. #endif
  1258. #ifdef LOSING_MSG
  1259. void msg(str,a1,a2,a3,a4,a5,a6)
  1260. char *str;
  1261. {
  1262.     fflush(msg_file);
  1263.     fprintf(stderr,"%s: ",tar);
  1264.     if(f_sayblock)
  1265.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1266.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1267.     putc('\n',stderr);
  1268.     fflush(stderr);
  1269. }
  1270.  
  1271. void msg_perror(str,a1,a2,a3,a4,a5,a6)
  1272. char *str;
  1273. {
  1274.     int save_e;
  1275.     extern int errno;
  1276.  
  1277.     save_e=errno;
  1278.     fflush(msg_file);
  1279.     fprintf(stderr,"%s: ",tar);
  1280.     if(f_sayblock)
  1281.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1282.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1283.     fprintf(stderr,": ");
  1284.     errno=save_e;
  1285.     perror(" ");
  1286. }
  1287.  
  1288. #endif
  1289.